#include "app_common.h"
#include "pthread.h"

int c;


void *fun1(void *arg)
{
    log_info("thread 1 start");
    while (1)
    {
        log_info("thread 1 run");
        if (c == 'a')
        {
            log_info("thread 1 exit");
            pthread_exit(NULL);
        }
        sleep(1);
    }
}

void *fun2(void *arg)
{
    log_info("thread 2 start");
    while (1)
    {
        log_info("thread 2 run");
        if (c == 'b')
        {
            log_info("thread 2 exit");
            pthread_exit(NULL);
        }
        sleep(1);
    }
}

int main(int argc, char const *argv[])
{
    /* 1. 创建一个线程 */
    pthread_t id1;
    pthread_t id2;
    pthread_create(&id1, NULL, fun1, NULL);
    pthread_create(&id2, NULL, fun2, NULL);

    // 从键盘读取数据
    while (1)
    {
        c = getchar();  // 从stdin读取数据
        getchar(); // 消耗掉 \n

        if (c == 'c')
        {
            pthread_cancel(id1);
            pthread_cancel(id2);

            break;
        }
    }

    // sleep(1);
    //  等待线程1执行完毕
    pthread_join(id1, NULL);
    pthread_join(id2, NULL);
    return 0;
}
